extends Npc
class_name BlackPlayer

signal half_hp

const FireBall := preload("res://Scenes/Character/Enemy/black_player/fire_ball.tscn")

@onready var gpu_particles_2d: GPUParticles2D = $Body/GPUParticles2D
@onready var gpu_particles_2d_2: GPUParticles2D = $Body/GPUParticles2D2
@onready var point_light_2d: PointLight2D = $Body/PointLight2D
@onready var hurt_box: Area2D = $Body/HurtBox
@onready var particles_manager: Node2D = $ParticlesManager
@onready var audio_stream_player_2d_2: AudioStreamPlayer2D = $AudioStreamPlayer2D2

@export var start_index:int
@export var follow_points:Array[Node2D]
@export var attack_cooldown:float = 3
@export var attack_speed:float = 800
@export var max_hp:int

var is_start:bool
var attack_timer:float
var current_hp:int

func _ready() -> void:
	super._ready()
	DialogHandler.dialogue_start.connect(on_dialogue_start.unbind(2))

func red_temp() -> void:
	animated_sprite_2d.modulate = Color.WHITE
	gpu_particles_2d.emitting = true
	gpu_particles_2d.emitting = true
	point_light_2d.visible = true
	is_start = true
	current_hp = max_hp
	change_animate("idle_change_fly")
	hurt_box.body_entered.connect(on_hurtbox_entered)

func on_dialogue_start() -> void:
	attack_cooldown = INF

func on_dialogue_end(cur_id: String) -> void:
	super.on_dialogue_end(cur_id)
	attack_timer = 0
	attack_cooldown = 3.0

func change_animate(ani_name:String) -> void:
	super.change_animate(ani_name)
	if ani_name == "cower_change_idle":
		animated_sprite_2d.position.y -= 20
		await animated_sprite_2d.animation_finished
		animated_sprite_2d.play("idle")
	elif ani_name == "idle_change_fly":
		await animated_sprite_2d.animation_finished
		animated_sprite_2d.play("fly_idle")
		is_start = true

func _physics_process(delta: float) -> void:
	if is_start:
		if current_hp > 0:
			set_heading()
			if follow_points.size() != 0:
				var next_point = follow_points[start_index]
				if global_position.is_equal_approx(next_point.global_position):
					animated_sprite_2d.play("fly_idle")
					if animated_sprite_2d.position != Vector2(-1,15):
						animated_sprite_2d.position = Vector2(-1,15)
					attack_timer += delta
					if attack_timer > attack_cooldown:
						attack_timer = 0
						var tween = create_tween()
						tween.tween_property(self,"modulate",Color(1.0, 0.0, 0.0, 1.0),.1)
						tween.tween_property(self,"modulate",Color(1.0, 1.0, 1.0, 1.0),.1)
						tween.tween_callback(attack)
						
				else:
					global_position = global_position.lerp(next_point.global_position, delta * 6)
					animated_sprite_2d.play("fly_dash")
					if animated_sprite_2d.position != Vector2(19,15):
						audio_stream_player_2d_2.play()
						animated_sprite_2d.position = Vector2(19,15)
					attack_timer = 0
		else:
			animated_sprite_2d.play("idle")
			velocity += get_gravity() * delta
			move_and_slide()
			if is_on_floor_only():
				is_start = false
				action()
	

func attack() -> void:
	var fire_ball := FireBall.instantiate()
	fire_ball.global_position = global_position-Vector2(0,60)
	fire_ball.set_velocity(Global.player.global_position-Vector2(0,60), attack_speed)
	get_parent().add_child(fire_ball)

func hurt() -> void:
	$AudioStreamPlayer2D.play()
	current_hp -= 1
	if current_hp == 0:
		collision_mask = 1
	elif current_hp == 4:
		half_hp.emit()
	var particles :Array = particles_manager.get_children()
	for i in particles:
		i.emitting = true

func on_hurtbox_entered(_body:Player) -> void:
	$AudioStreamPlayer2D.play()
	_body.add_dash()
	set_physics_process(false)
	start_index += 1
	var particles :Array = particles_manager.get_children()
	for i in particles:
		i.emitting = true
	await get_tree().create_timer(.1).timeout
	set_physics_process(true)
